package lc20240523;

import lc20240521.TreeNode;

import java.util.Deque;
import java.util.LinkedList;

public class Demo1 {
    public static void main(String[] args) {
        TreeNode root=new TreeNode(7);
        root.left=new TreeNode(3);
        root.right=new TreeNode(15);
        root.right.left=new TreeNode(9);
        root.right.right=new TreeNode(20);
        BSTIterator iterator=new BSTIterator(root);
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

}
class  BSTIterator{
    private TreeNode cur;
    private Deque<TreeNode> stack;

    public BSTIterator(TreeNode cur) {
        this.cur = cur;
        stack=new LinkedList<TreeNode>();
    }
    public boolean hasNext(){
        return cur!=null||!stack.isEmpty();
    }
    public int next(){
        while (cur!=null){
            stack.push(cur);
            cur=cur.left;
        }
        cur=stack.pop();
        int ret=cur.val;
        cur=cur.right;
        return ret;
    }
}